import { BuffEffect, EffectType, prop_min_type, taskKey } from "../shared/interface";
import { Bot_msg } from "./bot/bot_msg";
const icons = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟']
const fs = require('fs');
const path = require('path');
class func {
    pngDir = 'bot.nfiii.cn'
    server = '127.0.0.1'
    constructor() {

    }
    deleteFilesInDirectory(directoryPath):Promise<{ successCount: number, failCount: number }> {
        let succ = 0;
        let fail = 0;
        const _directoryPath = path.resolve(__dirname, directoryPath); // 构建相对路径
        return new Promise((resolve, reject) => {
            // 读取目录中的文件
            fs.readdir(_directoryPath, (err, files) => {
                if (err) {
                    console.error('Error reading directory:', err);
                    reject(err);
                    return;
                }

                // 遍历目录中的每个文件
                files.forEach(file => {
                    // 构建文件的完整路径
                    const filePath = path.join(_directoryPath, file);

                    // 删除文件
                    fs.unlink(filePath, err => {
                        if (err) {
                            fail += 1;
                            console.error('Error deleting file:', filePath, err);
                        } else {
                            succ += 1;
                            console.log('Deleted file:', filePath);
                        }

                        // 判断是否所有文件已处理完毕
                        if (succ + fail === files.length) {
                            resolve({ successCount: succ, failCount: fail });
                        }
                    });
                });
            });
        });
    }
    /**
* 字符串相识度算法
* @param x 
* @param y 
* @returns 
*/
    xsd(x: any, y: any) {
        let z = 0;
        x = x.toUpperCase();

        y = y.toUpperCase();
        x = x.replace('_', '');
        y = y.replace('_', '');
        if (typeof x == "string") {
            x = x.split("");
            y = y.split("");
        }
        let s = x.length + y.length;
        x.sort();
        y.sort();
        let a = x.shift();
        let b = y.shift();
        while (a !== undefined && b != undefined) {
            if (a === b) {
                z++;
                a = x.shift();
                b = y.shift();
            } else if (a < b) {
                a = x.shift();
            } else if (a > b) {
                b = y.shift();
            }
        }
        return z / s * 200;
    }
    /**
     * 将数字转为中文
     * @param number 
     * @returns 
     */
    numberToChinese(number) {
        const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        const chineseUnits = ['', '十', '百', '千', '万', '亿'];

        let result = '';

        // 将数字转换为字符串，便于处理每一位
        const numStr = String(number);

        // 从个位开始遍历数字字符串
        for (let i = 0; i < numStr.length; i++) {
            const digit = parseInt(numStr[i]); // 当前位的数字
            const unit = chineseUnits[numStr.length - 1 - i]; // 当前位的单位

            // 如果当前位的数字为零，且当前位不是最后一位，则添加一个零
            if (digit === 0 && i !== numStr.length - 1) {
                result += chineseNumbers[digit];
            } else {
                // 否则，添加当前位的数字和单位
                result += chineseNumbers[digit] + unit;
            }
        }

        // 去除可能出现的连续的零
        result = result.replace(/零+/g, '零');

        // 去除末尾的零
        result = result.replace(/零+$/, '');

        // 特殊情况处理，如果结果以"一十"开头，则去掉"一"
        if (result.startsWith('一十')) {
            result = result.substring(1);
        }

        return result;
    }
    /**
   * 
   * @param number 
   * @param decimalDigit 小数长度
   * @returns 
   */
    BN(number: any, decimalDigit?: any) {
        if (isNaN(Number(number))) {
            return number;
        }
        number = Number(number)
        decimalDigit = decimalDigit == null ? 2 : decimalDigit;
        let integer = Math.floor(number);
        let digit = this._getDigit(integer);
        // ['个', '十', '百', '千', '万', '十万', '百万', '千万']; 
        let unit: string[] = [];
        if (digit > 3) {
            let multiple = Math.floor(digit / 8);
            if (multiple >= 1) {
                let tmp = Math.round(integer / Math.pow(10, 8 * multiple));
                unit.push(this._addWan(tmp, number, 8 * multiple, decimalDigit) as any);
                for (let i = 0; i < multiple; i++) {
                    unit.push('亿');
                }
                return unit.join('');
            } else {
                return this._addWan(integer, number, 0, decimalDigit);
            }
        } else {
            return Math.ceil(number);
        }
    }
    private _addWan(integer: any, number: any, mutiple: any, decimalDigit: any) {
        let me = this;
        let digit = me._getDigit(integer);
        if (digit > 3) {
            let remainder = digit % 8;
            if (remainder >= 5) { // ‘十万’、‘百万’、‘千万’显示为‘万’ 
                remainder = 4;
            }
            return Math.round(number / Math.pow(10, remainder + mutiple - decimalDigit)) / Math.pow(10, decimalDigit) + '万';
        } else {
            return Math.round(number / Math.pow(10, mutiple - decimalDigit)) / Math.pow(10, decimalDigit);
        }
    }
    private _getDigit(integer: any) {
        let digit = -1;
        while (integer >= 1) {
            digit++;
            integer = integer / 10;
        }
        return digit;
    }
    /**
     * 时间戳转时分秒
     * @param time 
     * @returns 
     */
    getTimeStr(time: number) {
        const date = new Date(time);
        // 获取具体的年月日时分秒
        const year = date.getFullYear();
        const month = date.getMonth() + 1; // 月份从0开始，需要加1
        const day = date.getDate();
        const hours = date.getHours();
        const minutes = date.getMinutes();
        const seconds = date.getSeconds();
        return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hours}:${minutes}:${seconds}`
    }
    /**
    * 时间戳转时分秒
    * @param time 
    * @returns 
    */
    getTimeStr_h(time: number) {
        const date = new Date(time);
        // 获取具体的年月日时分秒
        const year = date.getFullYear();
        const month = date.getMonth() + 1; // 月份从0开始，需要加1
        const day = date.getDate();
        const hours = date.getHours();
        const minutes = date.getMinutes();
        const seconds = date.getSeconds();
        return `${hours}:${minutes}:${seconds}`
    }
    getTaskDesc(t: taskKey) {
        let desc = '';
        switch (t) {
            case taskKey.击杀普通怪物:
                desc = `⚔️[攻击]任意普通怪物并击杀N次`
                break;
            case taskKey.成功捕捉宠物:
                desc = `成功[捕捉]到N只宠物`
                break;
            case taskKey.移动到未知位置:
                desc = `移动探索N个位置`
                break;
            case taskKey.成功打开宝箱:
                desc = `🎁成功[打开宝箱]N次`
                break;
            case taskKey.签到一次:
                desc = `成功[签到]N次,每天一次哦`
                break;
            case taskKey.挂机时长:
                desc = `🕜️[挂机]N分钟`
                break;
            case taskKey.钓到鱼只:
                desc = `🐟️[钓鱼]钓到N只鱼`
                break;
            case taskKey.钓到鱼斤:
                desc = `🐟️[钓鱼]钓到N斤鱼`
                break;
            case taskKey.砍树次:
                desc = `🌲️[砍树]砍倒N棵树`
                break;
            default:
                desc = `未知任务,key:${t}`
                break;
        }
        return desc;
    }
    getPropType(t: number) {
        let obj = {
            icon: '',
            name: ''
        }
        switch (t) {
            case prop_min_type.道具:
                obj.icon = '📦'
                obj.name = '道具'
                break;
            case prop_min_type.装备:
                obj.icon = '⚔️'
                obj.name = '装备'
                break;
            case prop_min_type.技能书:
                obj.icon = '📘'
                obj.name = '技能书'
                break;
            case prop_min_type.宠物蛋:
                obj.icon = '🥚'
                obj.name = '宠物蛋'
                break;
            default:
                break;
        }
        return obj
    }
    /**
     * 将对象中全部数据只保留2位小数
     * @param obj 
     */
    roundNumbers(obj, num: number = 2) {
        for (const key in obj) {
            if (typeof obj[key] === 'number') {
                obj[key] = Number(obj[key].toFixed(num));
            } else if (typeof obj[key] === 'object') {
                this.roundNumbers(obj[key]); // 递归处理子对象
            }
        }
        return obj;
    }
    roundNumBn(obj) {
        for (const key in obj) {
            const element = obj[key];
            if (typeof (element) == 'number') {
                obj[key] = this.BN(element);
            } else if (typeof obj[key] === 'object') {
                this.roundNumBn(element);
            }
        }
        return obj;
    }
    getNumIcon(num: number) {
        if (typeof (icons[num]) == 'undefined') {
            return `[${num}]`
        }
        return icons[num]
    }
    /**
* 获取一个随机数
*/
    random(min: number, max: number) {
        return Math.floor(Math.random() * (max - min + 1)) + min
    }
    getEffectStyle(type: EffectType, val: number | string) {
        let style = '';
        switch (type) {
            case EffectType.对目标造成物理伤害:
                style = `🗡${this.BN(val)}`
                break;
            case EffectType.召唤永久单位:
                style = `✡️${val}`
                break;
            case EffectType.永久增加最大生命值:
                style = `🔺永久+${this.BN(val)}❤`
                break;
            case EffectType.恢复目标生命值:
                style = `💖+${this.BN(val)}`
                break;
            case EffectType.屏障:
                style = `🤍${this.BN(val)}`
                break;
            case EffectType.对目标造成魔法伤害:
                style = `🪄${this.BN(val)}`
                break;
            case EffectType.真实伤害:
                style = `☢️${this.BN(val)}`
                break;
            case EffectType.增加金币:
                style = `💰+${this.BN(val)}`
                break;
            default:

                break;
        }
        let buffKey;
        // buff 检查
        if (style.length <= 0) {
            switch (type) {
                case EffectType.buff_提升物理吸血:
                    buffKey = BuffEffect.物理吸血
                    break;
                case EffectType.buff_提升生命恢复:
                    buffKey = BuffEffect.生命恢复速度
                    break;
                case EffectType.buff_提升最大魔法值:
                    buffKey = BuffEffect.最大魔法值
                    break;
                case EffectType.buff_提升魔法值恢复速度:
                    buffKey = BuffEffect.魔法恢复速度
                    break;
                case EffectType.buff_提升最小物攻:
                    buffKey = BuffEffect.最小物攻
                    break;
                case EffectType.buff_提升最大物攻:
                    buffKey = BuffEffect.最大物攻
                    break;
                case EffectType.buff_提升攻击速度:
                    buffKey = BuffEffect.攻击速度
                    break;
                case EffectType.buff_提升最大魔攻:
                    buffKey = BuffEffect.最大魔攻
                    break;
                case EffectType.buff_提升最小魔攻:
                    buffKey = BuffEffect.最小魔攻
                    break;
                case EffectType.buff_提升魔法防御:
                    buffKey = BuffEffect.魔法防御
                    break;
                case EffectType.buff_提升物理防御:
                    buffKey = BuffEffect.物理防御
                    break;
                case EffectType.buff_提升魔法暴击率:
                    buffKey = BuffEffect.魔法暴击率
                    break;
                case EffectType.buff_提升物理暴击率:
                    buffKey = BuffEffect.物理暴击率
                    break;
                case EffectType.buff_提升魔法吸血:
                    buffKey = BuffEffect.魔法吸血
                    break;
                case EffectType.buff_提升最大生命:
                    buffKey = BuffEffect.最大生命

                    break;

                default:
                    break;
            }
            if (buffKey) {
                style = `🔺${val}点${buffKey}`;
            }
        }
        if (style.length <= 0) {
            style = `${type}${this.BN(val)}`
        }
        return style;
    }
    getAttName(key: string): string {
        switch (key) {
            case 'Hp_Max':
                return '生命值';
            case 'Hp_res':
                return '生命恢复速度';
            case 'Mp_Max':
                return '魔法值';
            case 'Mp_res':
                return '魔法恢复速度';
            case 'Physical_Attack_max':
                return '最大物理攻击';
            case 'Physical_Attack_min':
                return '最小物理攻击';
            case 'Physical_Ls':
                return '物理吸血';
            case 'Physical_crit':
                return '物理暴击率';
            case 'Physical_defense':
                return '物理防御';
            case 'Magic_Attack_max':
                return '最大魔法攻击';
            case 'Magic_Ls':
                return '魔法吸血';
            case 'Magic_defense':
                return '魔法防御';
            case 'Magic_crit':
                return '魔法暴击率';
            case 'Magic_Attack_min':
                return '最小魔法攻击';
            case 'Attack_Speed':
                return '攻击速度';
            case 'Calls_cont':
                return '召唤数量';
            case 'Calls_leveMax':
                return '召唤等级最高';
            case 'Calls_leveMin':
                return '召唤等级最低';
            case 'Calls_sore':
                return '召唤评分';
            default:
                break;
        }
        return `未知属性[${key}]`;
    }
}
export default new func();